//
//  GuideViewController.m
//  DuoDian
//
//  Created by 张卓 on 2017/8/16.
//  Copyright © 2017年 bigcastcloudShop. All rights reserved.
//

#import "GuideViewController.h"
#import "LoginViewController.h"
#import "HomeViewController.h"

@interface GuideViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *guideScrollView;
@property (nonatomic, strong) UIButton *immediatelyButton;

@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[UserDefaultsManager shareUserDefaultsManager] hasCurrentVersion]) {
        [self changerRootViewController];
    } else {
        [[UserDefaultsManager shareUserDefaultsManager] setCurrentVersion];
        [self.view addSubview:self.guideScrollView];
        [self.view addSubview:self.immediatelyButton];
    }
}

- (void)viewDidLayoutSubviews {
    self.guideScrollView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    self.immediatelyButton.frame = CGRectMake(0, SCREENHEIGHT - 100, 200, 50);
    self.immediatelyButton.center = CGPointMake(self.view.center.x, self.immediatelyButton.center.y);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    WS(weakSelf)
    if (scrollView.contentOffset.x / SCREENWIDTH == 2) {
        [UIView animateWithDuration:0.8 animations:^{
            weakSelf.immediatelyButton.alpha = 1.0f;
        } completion:nil];
    } else {
        weakSelf.immediatelyButton.alpha = 0.0f;
    }
}

#pragma mark - event response
- (void)didPressImmediatelyButton:(UIButton *)sender {
    [self changerRootViewController];
}

#pragma mark - private methods
- (void)changerRootViewController {
        HomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"HomeViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"Home"];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:homeVC];
        [UIApplication sharedApplication].delegate.window.rootViewController = nav;
}

#pragma mark - getters and setters
- (UIScrollView *)guideScrollView {
    if (!_guideScrollView) {
        _guideScrollView = [[UIScrollView alloc] init];
        _guideScrollView.contentSize = CGSizeMake(SCREENWIDTH * 3, SCREENHEIGHT);
        _guideScrollView.backgroundColor = [UIColor whiteColor];
        _guideScrollView.pagingEnabled = YES;
        _guideScrollView.bounces = NO;
        _guideScrollView.showsHorizontalScrollIndicator = NO;
        _guideScrollView.delegate = self;
        
        for (int i = 0; i < 3; i++) {
            UIImageView *guidePage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"guide_%d",i+1]]];
            guidePage.backgroundColor = [UIColor redColor];
            guidePage.frame = CGRectMake(i * SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT);
            [_guideScrollView addSubview:guidePage];
        }
    }
    return _guideScrollView;
}

- (UIButton *)immediatelyButton {
    if (!_immediatelyButton) {
        _immediatelyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _immediatelyButton.alpha = 0.0f;
        [_immediatelyButton setTitle:@"立即开始" forState:UIControlStateNormal];
        [_immediatelyButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_immediatelyButton addTarget:self action:@selector(didPressImmediatelyButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _immediatelyButton;
}

@end
